package com.echoesnet.echoesshortvideo.views.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Persistence.MyVideosInfoEntity;
import com.echoesnet.echoesshortvideo.Persistence.TinyVideoInfoEntity;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.Image.GlideApp;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

public class MyVideoListAdapter extends UltimateViewAdapter<UltimateRecyclerviewViewHolder> {
    private String TAG = MyVideoListAdapter.class.getSimpleName();

    private static final int VIEW_TYPE_VIDEO = 1000;
    private Activity mAct;
    private List<MyVideosInfoEntity> dataList;

    private int itemW, itemH;

    public MyVideoListAdapter(Activity act, List<MyVideosInfoEntity> list) {
        this.mAct = act;
        this.dataList = list;
//        itemW = (CommonUtils.getScreenWidth(mAct)-CommonUtils.dp2px(mAct, 1)) / 2;
        itemW = CommonUtils.getScreenWidth(mAct) / 2;
            //<!-- 374px * 480 px -->
            //<!-- 高是宽的1.28倍 -->
        itemH = (int)(itemW * 1.28);
    }

    // 复写 UltimateViewAdapter 增加自己类型------------------------------------------------------
    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(VIEW_TYPE_VIDEO == viewType){
            View view = LayoutInflater.from(mAct).inflate(R.layout.item_my_video_list, parent, false);
            view.setLayoutParams(new ViewGroup.LayoutParams(itemW,itemH));



            view.findViewById(R.id.ivCoverImg).setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(v,(MyVideosInfoEntity)v.getTag(R.id.viewTagFirst));
                }
            });

            return new UltimateRecyclerviewViewHolder(view);
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public int getItemViewType(int position) {
        if(VIEW_TYPES.NORMAL != super.getItemViewType(position)){
            return super.getItemViewType(position);
        }else {
//            int fixPosition = position + (getItemCount() - getAdapterItemCount()); // 当前位置 补偿 总item与内容item的差值
//            if(enableLoadMore())
//                fixPosition -=1;    //footer 存在 刚才就加多了

            return VIEW_TYPE_VIDEO;
        }
    }

    public void setDataList(List<MyVideosInfoEntity> dataList) {
        this.dataList = dataList;
    }
    // 无用
    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent) {
        return null;
    }
    // END----------------- 复写 UltimateViewAdapter 增加自己类型------------------------------------------------------




    @Override
    public void onBindViewHolder(UltimateRecyclerviewViewHolder viewHolder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_VIDEO) {
            bindDefaultTileItem(position, viewHolder);
        }
//        else if (getItemViewType(position) == VIEW_TYPE_GALLERY) {
//            final GalleryViewHolder holder = (GalleryViewHolder) viewHolder;
//            bindLoadingGallery(holder);
//        }
    }


    private void bindDefaultTileItem(int position, UltimateRecyclerviewViewHolder holder) {
        MyVideosInfoEntity videoInfo = dataList.get(position);

        TextView tvLoveNum = holder.getView().findViewById(R.id.tvLoveNum);
        ImageView ivCoverImg = holder.getView().findViewById(R.id.ivCoverImg);


        tvLoveNum.setText(videoInfo.getLikeNum());

        //给listen加值，在 onCreateViewHolder 里；

        tvLoveNum.setTag(R.id.viewTagFirst,videoInfo);
        ivCoverImg.setTag(R.id.viewTagFirst,videoInfo);


        GlideApp.with(AppController.getInstance())
                .load(videoInfo.getThumbnails())
                .centerCrop()
                .placeholder(R.drawable.bg_live_default)
                .error(R.drawable.bg_live_default)
                .into(ivCoverImg);


    }



    @Override
    public long getItemId(int position) {
        return position;
    }



    private ItemClickListener onItemClickListener;
    public void setOnItemClickListener(ItemClickListener l) {
        this.onItemClickListener = l;
    }

    public interface ItemClickListener{
        void onClick(View v, MyVideosInfoEntity tag);
    }



    @Override
    public UltimateRecyclerviewViewHolder newFooterHolder(View view) {
        return null;
    }

    @Override
    public UltimateRecyclerviewViewHolder newHeaderHolder(View view) {
        return null;
    }


    @Override
    public int getAdapterItemCount() {
        return dataList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return 0;
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }



}
